package com.wanwu.channel.controller;

import com.wanwu.channel.biz.PayOrderService;
import com.wanwu.channel.biz.po.PayOrder;
import com.wanwu.channel.controller.resp.ResponseObject;
import com.wanwu.channel.model.AccountPayReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.guangdian.ota.controller.WXPayReq;
import javax.annotation.Resource;

/**
 * 确认收货
 */
@RestController
@Slf4j
@RequestMapping("/payOrder")
public class PayOrderController {

    @Resource
    private PayOrderService payOrderService;

    /**
     * 支付
     */
    @PostMapping()
    @ResponseBody
    public ResponseObject pay(@RequestBody WXPayReq req) throws Exception {
        int statusCode = callWxPay(req);
        req.setResultCode(String.valueOf(statusCode));
        if (statusCode == 200) { //处理成功
            req.setResult(1);
        } else{
            req.setResult(2);
        }
        PayOrder payOrder = payOrderService.insertWxPayOrder(req);
        ResponseObject responseObject = new ResponseObject();
        responseObject.setCode("10000");
        responseObject.setMessage("成功");
        responseObject.setData(payOrder.getPayNo());
        return responseObject;
    }

    /**
     * 余额支付
     */
    @PostMapping("/account/pay")
    @ResponseBody
    public ResponseObject accountPay(@RequestBody AccountPayReq req) throws Exception {
        ResponseObject responseObject = new ResponseObject();
        responseObject.setCode("10000");
        responseObject.setMessage("成功");
        try {
            // 调用余额支付
            payOrderService.accountPay(req);
        } catch (Exception e) {
            log.error("余额支付", e);
            responseObject.setCode("20001");
            responseObject.setMessage("参数错误");
        }
        return responseObject;
    }

    private int callWxPay(WXPayReq req){
        return 200;
    }

}